﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net;
using System.Collections.Generic;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

public partial class Alipay_GoAliNotify : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Hangjing.Model.acountInfo mymodel = SectionProxyData.getOnlinePayType(1);
        string alipayNotifyURL = "https://mapi.alipay.com/gateway.do?service=notify_verify";
        string key = mymodel.Ali_Key; //partner 的对应交易安全校验码（必须填写）
        string partner = mymodel.Ali_Partner; 		//partner合作伙伴id（必须填写）
        string _input_charset = "utf-8";//编码类型，完全根据客户自身的项目的编码格式而定，千万不要填错。否则极其容易造成MD5加密错误。

        alipayNotifyURL = alipayNotifyURL + "&partner=" + partner + "&notify_id=" + Request.Form["notify_id"];

        //获取支付宝ATN返回结果，true是正确的订单信息，false 是无效的
        string responseTxt = Get_Http(alipayNotifyURL, 120000);
        int i;
        NameValueCollection coll;
        //Load Form variables into NameValueCollection variable.
        coll = Request.Form;

        // Get names of all forms into a string array.
        String[] requestarr = coll.AllKeys;

        //进行排序；
        string[] Sortedstr = AliPay.BubbleSort(requestarr);

        //构造待md5摘要字符串 ；
        string prestr = "";
        for (i = 0; i < Sortedstr.Length; i++)
        {
            if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
            {
                if (i == Sortedstr.Length - 1)
                {
                    prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]];
                }
                else
                {
                    prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]] + "&";
                }
            }
        }
        prestr = prestr + key;

        string mysign = AliPay.GetMD5(prestr, _input_charset);

        string sign = Request.Form["sign"];

        if (mysign == sign && responseTxt == "true")   //验证支付发过来的消息，签名是否正确，只要成功进如这个判断里，则表示该页面已被支付宝服务器成功调用
        //但判断内出现自身编写的程序相关错误导致通知给支付宝并不是发送success的消息的情况或没有更新客户自身的数据库，请自身程序编写好应对措施，否则查明原因时困难之极
        {
            if (Request.Form["trade_status"] == "WAIT_BUYER_PAY")//   判断支付状态_等待买家付款（文档中有枚举表可以参考）            
            {
                //更新自己数据库的订单语句，请自己填写一下
                string strOrderNO = Request.Form["out_trade_no"];//订单号
                string strPrice = Request.Form["total_fee"];//金额

                Response.Write("success");
            }
            else if (Request.Form["trade_status"] == "TRADE_FINISHED" || Request.Form["trade_status"] == "TRADE_SUCCESS")//高级即时到帐状态下 TRADE_SUCCESS
            //判断支付状态_TRADE_FINISHED交易成功（文档中有枚举表可以参考）
            //如果trade_status的值是TRADE_SUCCESSED，是因为您用的测试帐号是返回的这个结果，改成您自己的支付宝帐号和PID以及KEY，则返回的值才是TRADE_FINISHED            
            {
                //更新到数据库的订单
                string strOrderNO = Request.Form["out_trade_no"];//订单号
                string strPrice = Request.Form["total_fee"];     //金额

                Hangjing.AppLog.AppLog.Info("支付里面日志订单编号：" + strOrderNO);

                int leng = strOrderNO.Length;
                string myorderid = strOrderNO.Substring(0, leng - 3);//系统中用的订单编号

                string firststr = strOrderNO.Substring(0, 1);
                switch (firststr)
                {
                    case "r"://充值
                        {
                            UserAddMoneyLog udal = new UserAddMoneyLog();
                            UserAddMoneyLogInfo uinfo = new UserAddMoneyLogInfo();
                            uinfo.Inve2 = myorderid;
                            uinfo.AddMoney = Convert.ToDecimal(strPrice);
                            udal.CallBlackByAli(uinfo);
                        }
                        break;
                    case "e": //跑腿
                        {
                            //订单支付
                            //获取订单列表(存在多个订单同时提交的情况)
                            IList<ExpressOrderInfo> list = new List<ExpressOrderInfo>();
                            ExpressOrder dal = new ExpressOrder();

                            list = dal.GetList(100, 1, "Orderid='" + myorderid + "'", "dataid", 1);

                            ///更新订单状态
                            foreach (ExpressOrderInfo info in list)
                            {
                                info.OrderID = info.OrderID;
                                info.PayMode = 1;
                                info.paymoney = info.TotalPrice;
                                info.paystate = 1;
                                info.paytime = DateTime.Now;
                                info.Currentprice = 0;
                                dal.UpdatePayState(info);
                            }
                        }
                        break;
                    default:
                        {
                            //订单支付
                            //获取订单列表(存在多个订单同时提交的情况)
                            IList<CustorderInfo> list = new List<CustorderInfo>();
                            Custorder dal = new Custorder();
                            ETogoLocal etdal = new ETogoLocal();

                            Points pdal = new Points();
                            PointsInfo pinfo = new PointsInfo();

                            ETogoLocalInfo etinfo = new ETogoLocalInfo();

                            list = dal.GetList(100, 1, "Orderid='" + myorderid + "'", "Unid", 1);

                            ///更新订单状态
                            foreach (CustorderInfo info in list)
                            {   
                                //避免多次通知，需要判断一下可能之前已经更新订单状态
                                if (info.paystate == 0)
                                {
                                    //支付成功后通知商家
                                    //2015.3.25 货到付款和余额支付的直接通知商家,支付宝支付 微信支付 银联支付需要支付成功后再通知
                                    NoticeHelper notice = new NoticeHelper(Context, info.TogoId.ToString());
                                    notice.send2ShopByShopid();

                                    //自动调度订单 通知骑士
                                    string isoepnauto = CacheHelper.GetSetValue(57);
                                    if (isoepnauto == "1")
                                    {
                                        //货到付款或者账户余额支付的 这里进行自动调度，支付宝、微信支付的需要支付成功后调度 2015.3.25 zjf@ihangjing.com
                                        {
                                            //自动调度给最近的配送员（只有统一配送的才调度）
                                            pinfo = pdal.GetModel(info.TogoId);
                                            etinfo = etdal.GetInfoById(info.TogoId.ToString());
                                            if (pinfo.sentorg == "0")
                                            {
                                                int did = dal.OutsetOrderDeliver(info.orderid, etinfo.Lat, etinfo.Lng, pinfo.cityid);

                                                if (did > 0)
                                                {
                                                    OrderCountInfo model = new Custorder().SendDeliverOrderCount(did.ToString());
                                                    string msg = "{\"state\":\"1\",\"newordercount\":\"" + model.CountIntValue + "\",\"ordercount\":\"" + 0 + "\",\"ordercountall\":\"" + 0 + "\",\"ExpressOrdercount\":\"0\"}";
                                                    HjNetHelper.SendMsg2Android(did.ToString(), 0, 1, msg, CacheHelper.GetSetValue(59));
                                                }
                                            }
                                        }
                                    }


                                    //发送短信给商家和用户
                                    //您好，【变量】，您的订单【变量】已经提交成功【变量】
                                    //新订单，订单号【变量】，【变量】
                                    //订单号：【变量】【变量】
                                    //try
                                    //{
                                    //    HJMSG.SendOrderTogoByOrderId(info.orderid);
                                    //}
                                    //catch (Exception ex)
                                    //{
                                    //    HJlog.toLog(ex.ToString());
                                    //}
                                }

                                info.orderid = info.orderid;
                                info.paymode = 1;
                                info.paymoney = info.OrderSums;
                                info.paystate = 1;
                                info.paytime = DateTime.Now;
                                dal.UpdatePayState(info);
                            }
                        }
                        break;
                }
                Response.Write("success");
            }
            else
            {
                //更新自己数据库的订单语句，请自己填写一下
                string strOrderNO = Request.Form["out_trade_no"];//订单号
                string strPrice = Request.Form["total_fee"];//金额
                Response.Write("faild");
            }

        }
        else
        {
            Response.Write("fail");

            //最好写TXT文件，以记录下是否异步返回记录。

            //写文本，纪录支付宝返回消息，比对md5计算结果（如网站不支持写txt文件，可改成写数据库）
            string TOEXCELLR = "MD5结果:mysign=" + mysign + ",sign=" + sign + ",responseTxt=" + responseTxt;
            StreamWriter fs = new StreamWriter(Server.MapPath("Log/" + DateTime.Now.ToString().Replace(":", "")) + ".txt", false, System.Text.Encoding.Default);
            fs.Write(TOEXCELLR);
            fs.Close();
        }
    }

    //获取远程服务器ATN结果，验证是否是支付宝服务器发来的请求
    public String Get_Http(String a_strUrl, int timeout)
    {
        string strResult;
        try
        {
            HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
            myReq.Timeout = timeout;
            HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
            Stream myStream = HttpWResp.GetResponseStream();
            StreamReader sr = new StreamReader(myStream, Encoding.Default);
            StringBuilder strBuilder = new StringBuilder();
            while (-1 != sr.Peek())
            {
                strBuilder.Append(sr.ReadLine());
            }

            strResult = strBuilder.ToString();
        }
        catch (Exception exp)
        {

            strResult = "错误：" + exp.Message;
        }

        return strResult;
    }
}
